package com.dhms.vostok1.data.device_center

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.dhms.vostok1.data.*
import com.squareup.moshi.Json

data class DeviceListQueryData(
    @Json(name = "de_is_enabled") val deIsEnabled: String? = "yes",
    @Json(name = "de_category_cu_id") val deCategoryCuId: Int? = null,
    @Json(name = "de_id") val deId: Long? = null, // origin type: number | string
    @Json(name = "de_has_sensor") var deHasSensor: String? = null,
    @Json(name = "spot_check") val spotCheck: String? = null,
    @Json(name = "de_show_status") val deShowStatus: String? = null,
    @Json(name = "order_by") val orderBy: String? = null,
    @Json(name = "de_consumer_id") val deConsumerId: Int? = null,
    @Json(name = "show_device_chart") val showDeviceChart: String? = true.toString(),
    @Json(name = "de_name") val deName: String? = null,
    @Json(name = "ble_beacons") val bleBeacons: String? = null,
    override val limit: Int = DEFAULT_PAGE_LIMIT,
    override val offset: Int = DEFAULT_PAGE_OFFSET,
) : BasePageQueryData

@Entity(tableName = "device")
data class DeviceListItemData(
    @PrimaryKey @ColumnInfo(name = "de_id") @Json(name = "de_id") val deId: Long,
    @ColumnInfo(name = "ct_id") @Json(name = "ct_id") val ctId: Int,
    @ColumnInfo(name = "ct_name") @Json(name = "ct_name") val ctName: String,
    @ColumnInfo(name = "cu_name") @Json(name = "cu_name") val cuName: String,
    @ColumnInfo(name = "de_consumer_id") @Json(name = "de_consumer_id") val deConsumerId: Long,
    @ColumnInfo(name = "de_created") @Json(name = "de_created") val deCreated: String,
    @ColumnInfo(name = "de_department_id") @Json(name = "de_department_id") val deDepartmentId: Long,
    @ColumnInfo(name = "de_extending") @Json(name = "de_extending") val deExtending: Map<String, Array<String>>,
    @ColumnInfo(name = "de_fault_count") @Json(name = "de_fault_count") val deFaultCount: Int,
    @ColumnInfo(name = "de_has_sensor") @Json(name = "de_has_sensor") val deHasSensor: String,
    @ColumnInfo(name = "de_is_enabled") @Json(name = "de_is_enabled") val deIsEnabled: String,
    @ColumnInfo(name = "de_manufacturer_id") @Json(name = "de_manufacturer_id") val deManufacturerId: Int?,
    @ColumnInfo(name = "de_manufacturer_name") @Json(name = "de_manufacturer_name") val deManufacturerName: String,
    @ColumnInfo(name = "de_model") @Json(name = "de_model") val deModel: String,
    @ColumnInfo(name = "de_name") @Json(name = "de_name") val deName: String,
    @ColumnInfo(name = "de_show_status") @Json(name = "de_show_status") val deShowStatus: Int,
    @ColumnInfo(name = "de_spot_check") @Json(name = "de_spot_check") val deSpotCheck: Int,
    @ColumnInfo(name = "de_state") @Json(name = "de_state") val deState: String,
    @ColumnInfo(name = "de_tpm_id") @Json(name = "de_tpm_id") val deTpmId: Int?,
    @ColumnInfo(name = "de_working_condition") @Json(name = "de_working_condition") val deWorkingCondition: Int,
    @ColumnInfo(name = "de_working_status") @Json(name = "de_working_status") val deWorkingStatus: Int,
    @ColumnInfo(name = "dp_name") @Json(name = "dp_name") val dpName: String,
    @ColumnInfo(name = "de_asset_ct_id") @Json(name = "de_asset_ct_id") val deAssetCtId: Int?,
    @ColumnInfo(name = "dpc_device_parent_id") @Json(name = "dpc_device_parent_id") val dpcDeviceParentId: Int?,
    @ColumnInfo(name = "device_tree_chart_config") @Json(name = "device_tree_chart_config") val deTreeChartConfig: String?,
    //故障
    @ColumnInfo(name = "fault_sum") @Json(name = "fault_sum") val faultSum: Int?,
    //隐患
    @ColumnInfo(name = "peril_sum") @Json(name = "peril_sum") val perilSum: Int?
)

typealias DeviceListResData = SecondaryBaseListData<DeviceListItemData>

data class ParamPointInfo(
    val uuid: String,
    @Json(name = "part_name") val partName: String,
    @Json(name = "node_type") val nodeType: String,
    @Json(name = "solid_node_data") val solidNodeData: Map<String, String>,
    val width: Int,
    val height: Int,
    val angle: Int,
    val x: Int,
    val y: Int,
)

data class DeviceMonitorItem(
    @Json(name = "cp_id") val cpId: Long?,
    @Json(name = "cp_name") val cpName: String?,
    @Json(name = "fa_id") val faId: Long?,
    @Json(name = "fa_level_no") val faLevelNo: Int,
    val id: Int,
    @Json(name = "last_time") val lastTime: String?,
    @Json(name = "last_value") val lastValue: Double?,
    val interval: Int?,
    @Json(name = "metric_no") val metricNo: String,
    @Json(name = "param_name") val paramName: String,
    @Json(name = "part_id") val partId: Int?,
    @Json(name = "part_name") val partName: String,
    val priority: String,
    val thresholds: String?,
    @Json(name = "thresholds_ext") val thresholdsExt: String?,
    @Json(name = "tpmp_id") val tpmpId: Int?,
    @Json(name = "tpmp_name") val tpmpName: String?,
    val unit: String,
    @Json(name = "use_type") val useType: Int,
    val isFake: Boolean?,
)

data class DeviceMonitorResData(
    @Json(name = "de_has_sensor") val deHasSensor: String,
    @Json(name = "realtime-params") val realtimeParams: List<DeviceMonitorItem>
)

data class DevicePartData(
    @Json(name = "cp_id") val cpId: Long,
    @Json(name = "cp_name") val cpName: String?,
    var collectionParams: MutableList<DeviceCollectionParamItem>,
    var currentMetricNo: String? = null,
    var currentMetricName: String? = null,
    var currentMetricUnit: String? = null,
    var currentMetricData: Map<String, TSDBResSourcesData>? = null,
    var currentMetricTag: String? = null,
    var isMetricDataLoading: Boolean = false,
    val point: List<Double>? = null,
    val size: List<Double>? = null,
)

data class DeviceCollectionParamItem(
    val id: Int?,
    @Json(name = "collection_point_param_id") val collectionPointParamId: Long,
    @Json(name = "collection_point_id") val collectionPointId: Long,
    @Json(name = "cp_name") val cpName: String?,
    @Json(name = "tpmp_id") val tpmpId: Int?,
    @Json(name = "tpmp_name") val tpmpName: String?,
    @Json(name = "param_name") val paramName: String?,
    @Json(name = "param_kind") val paramKind: String,
    @Json(name = "metric_no") val metricNo: String,
    val thresholds: String?,
    @Json(name = "thresholds_ext") val thresholdsExt: String?,
    val unit: Int?,
    @Json(name = "unit_name") val unitName: String?,
    @Json(name = "unit_title") val unitTitle: String?,
    @Json(name = "param_tags") val paramTags: String,
    @Json(name = "pa_source") val paSource: String,
    @Json(name = "pa_precision") val paPrecision: Int,
    @Json(name = "param_lower") val paramLower: String,
    @Json(name = "param_upper") val paramUpper: String,
    @Json(name = "pa_usetype") val paUsetype: Int?,
    var metricData: Map<String, TSDBResSourcesData>? = null,
    var isMetricDataLoading: Boolean = false,
)

data class DeviceCollectionPointItem(
    @Json(name = "cp_device_id") val cpDeviceId: Long,
    @Json(name = "cp_id") val cpId: Long,
    @Json(name = "cp_name") val cpName: String,
    @Json(name = "cp_part_id") val cpPartId: Long?,
    @Json(name = "cp_part_name") val cpPartName: String,
    /**
     * 测点关联的部件uuid
     */
    @Json(name = "device_part_ids") val devicePartIds: List<String>?,
    /**
     * 测点关联的部件名称
     */
    @Json(name = "device_part_names") val devicePartNames: List<String>?,
)

data class DeviceDynamicMonitorQueryData(
    @Json(name = "device_id") val deviceId: String? = null,
    @Json(name = "dp_type") val dpType: String? = null,
    @Json(name = "de_department_id") val deDepartmentId: String? = null,
    @Json(name = "fake_part") val fakePart: String? = null,
    val action: String? = null,
)

data class DeviceDynamicMonitorResData(
    @Json(name = "collection_params") val collectionParams: List<DeviceCollectionParamItem>,
    @Json(name = "collection_points") val collectionPoints: List<DeviceCollectionPointItem>,
    val config: String?,
    val extending: Any?
)

data class DeviceStateStatisticSensorData(
    @Json(name = "de_has_sensor") val hasSensor: String,
    val count: Int
)

data class DeviceStateStatisticStateData(
    @Json(name = "device_state") val deviceState: Int,
    val count: Int
)

data class DeviceStateStatisticResData(
    @Json(name = "has_sensor") val hasSensor: List<DeviceStateStatisticSensorData?>?,
    @Json(name = "state") val hasSensorState: List<DeviceStateStatisticStateData?>? = listOf(),
    @Json(name = "nosensor_state") val noSensorState: List<DeviceStateStatisticStateData?> = listOf(),
)

typealias DeviceStateStatisticData = Map<Int, Int>

data class NodeInfo(
    // 节点id
    val uuid: String,
    // 部件名称
    val part_name: String,
    // 节点类型
    @Json(name = "node_type") val nodeType: String,
    // x
    val x: Float?,
    // y
    val y: Float?,
    // 节点宽度
    val width: Float?,
    // 节点高度
    val height: Float?,
    // 节点旋转角度
    val angle: Float?,
)

data class DeviceTreeChartConfig(
    /**
     * 部件的缩略图bate64数据
     */
    val icon: String,
    /**
     * 节点信息map
     */
    @Json(name = "node_info_map") val nodeInfoMap: Map<String, NodeInfo>,
    /**
     * 测点id与测点节点关联map
     * key: 测点id
     * value: 测点节点uuid
     */
    @Json(name = "cp_node_id_map") val cpNodeIdMap: Map<String, String>,
)

data class TopLevelCategoryData(
    @Json(name = "id") val ct_id: Int,
    @Json(name = "name") val ct_name: String?,
    @Json(name = "device_count") val deviceCount: Int?,
)

data class DeviceCollectPointsDrawingData(
    val size: List<Double>?,
    val drawingData: Map<Long, List<Double>?>,
)

data class DevicePointData(
    @Json(name = "collect_id") val collectId: Long,
    @Json(name = "peril_sum") val perilSum: Int,
    @Json(name = "peril_id") val perilId: String,
    @Json(name = "fault_sum") val faultSum: Int,
    @Json(name = "fault_id") val faultId: String,
)

data class DevicePointListResData(
    val items: List<DevicePointData>? = null,
)
